import pandas as pd

# 练习 1: 读取 CSV 文件
df = pd.read_csv("sales_data.csv")
print("练习 1 答案:")
print(df.head(3))
print("\n")

# 练习 2: 基本信息查看
print("练习 2.1 答案 (DataFrame 信息):")
print(df.info())
print("\n练习 2.2 答案 (统计摘要):")
print(df.describe())
print("\n")

# 练习 3: 数据选择
print("练习 3.1 答案 (Electronics 类别):")
electronics = df[df["category"] == "Electronics"]
print(electronics)
print("\n练习 3.2 答案 (价格>100且数量<10):")
filtered = df[(df["price"] > 100) & (df["quantity"] < 10)]
print(filtered)
print("\n")

# 练习 4: 添加新列
df["total_sales"] = df["price"] * df["quantity"]
print("练习 4 答案 (添加 total_sales 列):")
print(df)
print("\n")


# 练习 5: 数据筛选与保存
high_sales = df[df["total_sales"] > 1000]
high_sales.to_csv("high_sales.csv", index=False)
print("练习 7 答案 (高销售额记录已保存到 high_sales.csv):")
print(high_sales)
